package org.xiji.Prototype2;



import java.io.*;

public class StudentC implements Serializable,Cloneable{
    private String name;
    private int age;

    public StudentC(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public StudentC() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    /**
     * 使用Cloneable接口，直接调用父接口的方法
     */
    public StudentC cloneByCloneable() {
        try {
            // 调用父接口的克隆方法 ===》并且进行强转
            return (StudentC) super.clone();

        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 通过java字节流流克隆对象
     */

    public StudentC cloneByStream() {
        try {

            ByteArrayOutputStream outPutStream = new ByteArrayOutputStream();

            ObjectOutputStream objectOutputStream = new ObjectOutputStream(outPutStream);

            objectOutputStream.writeObject(this);


            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(outPutStream.toByteArray());


            ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);


            return (StudentC) objectInputStream.readObject();


        } catch (Exception e) {
            e.printStackTrace();

        }



        return null;
    }

    /**
     * 通过创建对象的方式克隆
     * 如果对象的属性少话，还行，但是如果对象的多的话就不如上面的那种方法
     */
    public StudentC cloneByNew() {
        return new StudentC(this.name, this.age);
    }
}
